import useSuffixFileNameFormStore from '@renderer/store/Form/SuffixFileNameFormStore'

// 扩展名
export default function useSuffixFileNameTransfer(
  callback: () => ITableDataItem[]
) {
  const suffixFileNameTransfer = () => {
    const tableData = callback()

    const {
      suffixNameEnable,
      suffixNameType,
      newSuffixName,
      suffixNameSearch,
      suffixNameReplace
    } = useSuffixFileNameFormStore()

    // 启用扩展名
    if (!suffixNameEnable) return

    // 重置后缀
    function resetSuffix() {
      tableData.forEach((item) => {
        item.newest.ext = item.original.ext
      })
    }

    // 查找
    if (suffixNameType === '0') {
      if (newSuffixName) {
        tableData.forEach((item) => {
          item.newest.ext = newSuffixName
        })
      } else {
        resetSuffix()
      }
    } else {
      resetSuffix()
      // 替换
      if (suffixNameSearch && suffixNameReplace) {
        tableData.forEach((item) => {
          item.newest.ext = item.newest.ext.replaceAll(
            suffixNameSearch,
            suffixNameReplace
          )
        })
      }
    }
  }

  return {
    suffixFileNameTransfer
  }
}
